package com.example.demo.service.serviceImpl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.WaresManage;
import com.example.demo.entity.WaresManageDetail;
import com.example.demo.mapper.WaresManageDetailMapper;
import com.example.demo.service.WaresManageDetailService;
import com.example.demo.service.WaresManageService;
import com.example.demo.utils.CheckUtil;
import com.example.demo.utils.TimeUtil;
import com.example.demo.utils.UserInfoUtil;
import com.example.demo.vo.WaresManageDetailExport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "waresManageDetailService")
public class WaresManageDetailServiceImpl extends ServiceImpl<WaresManageDetailMapper, WaresManageDetail> implements WaresManageDetailService {
    @Autowired
    private WaresManageDetailMapper waresManageDetailMapper;

    @Autowired
    private WaresManageService waresManageService;
    /**
     * 添加明细
     * 并修改库存
     * @param waresManageDetail
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addWaresManageDetail(WaresManageDetail waresManageDetail) {
        //添加明细
        String userName = UserInfoUtil.gerUserName();
        String dateTime = TimeUtil.getDateTime();
        Integer newStock = 0;
        try {
            newStock = getNewStock(waresManageDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        waresManageDetail.setByName(userName);
        waresManageDetail.setCreateDate(dateTime);
        waresManageDetail.setDel(0);
        waresManageDetail.setOldStock(newStock);
        waresManageDetailMapper.insert(waresManageDetail);



        //修改库存
        WaresManage waresManage = new WaresManage();
        waresManage.setEditName(userName);
        waresManage.setUpdateDate(dateTime);
        waresManage.setId(waresManageDetail.getWaresId());
        waresManage.setStock(newStock);


        waresManageService.editWaresManage(waresManage);
        return true;
    }

    /**
     * 分页 联表查询
     * @param page
     * @return
     */
    @Override
    public IPage<List<WaresManageDetailExport>> getWaresManageDetailPage(Page page) {
        return waresManageDetailMapper.selectWaresManageDetailPage(page);
    }

    /**
     * 获取统计报表
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<Map<String, String>> getWaresManageDetailReport(String startTime, String endTime) {
        return waresManageDetailMapper.selectWaresManageDetailReport(startTime,endTime);
    }

    /**
     * 获得修改偏移量
     */
    public Integer getNumber(WaresManageDetail waresManageDetail) throws Exception{
        Integer detailType = waresManageDetail.getDetailType();
        Integer integer = 1;
        if(CheckUtil.checkObject(detailType)){
            throw new Exception("空数据异常");
        }

        Map<Integer,Integer> map = new HashMap<>();
        map.put(1,1);
        map.put(2,-1);

        if(map.containsKey(detailType)){
             integer = map.get(detailType);
        }else{
            throw new Exception("map中无定义该类型，需手动定义");
        }

        Integer num = integer * waresManageDetail.getNumber();
        return num;
    }

    /**
     * 获得新的库存量
     *
     * 之前的库存量 - 偏移量
     * @return
     */
    public Integer getNewStock(WaresManageDetail waresManageDetail) throws Exception{
        WaresManage byId = waresManageService.getById(waresManageDetail.getWaresId());

        int i = byId.getStock() - getNumber(waresManageDetail);

        if(i < 0){
            throw new Exception("库存量不足");
        }else{
            return i;
        }
    }

}
